import * as moment from 'moment';

/** 按顺序只是promise */
export function awaitWrapType<T>(promise: any): [any, T] {
  return promise
    .then((data: T) => [undefined, data])
    .catch((err: any) => [err, undefined]);
}

/** 格式化当前时间 */
export const curTime = () => {
  return `[${moment().format('yyyy-MM-DD HH:mm:sss')}]`;
};

/**
 * 生成随机数
 * @returns
 */
export const randomStr = () => {
  return Math.random().toString(36).slice(2);
};


/** 带时间输出log */
export const log = (...args: any) => {
  console.log(curTime(), ...args);
};

/** 延迟 */
export const delay = async (times: number) => {
  console.log(curTime(), `开始等待${times}s`);
  return new Promise(async (resolve) => {
    setTimeout(() => {
      resolve(0);
    }, times * 1000);

  });
};